/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package http;

import java.net.URI;
import snobject.snObject;

public class Request
{
	public static Request GET(URI Target, 
				  String[] Accept, String[] Expect) {
		return new Request(Target, "GET", null, null, Accept, Expect);
	}
	
	public static Request POST(URI Target, 
				   String PayloadType, byte[] Payload,
				   String[] Accept, String[] Expect) {
		return new Request(Target, "POST", 
				   PayloadType, Payload, 
				   Accept, Expect);
	}

	/* use the Xml encoding */
	public static Request POST(URI Target, 
				   snObject object,
				   String[] Accept, String[] Expect) {
		return new Request(Target, "POST", 
				   object.getXmlMimeType(), object.getXml(),
				   Accept, Expect);
	}
	
	public static Request PUT(URI Target, 
				  String PayloadType, byte[] Payload,
				  String[] Accept, String[] Expect) {
		return new Request(Target, "PUT", 
				   PayloadType, Payload, 
				   Accept, Expect);
	}

	public static Request PUT(URI Target, 
				  snObject object,
				  String[] Accept, String[] Expect) {
		return new Request(Target, "PUT", 
				   object.getXmlMimeType(), object.getXml(),
				   Accept, Expect);
	}

	public static Request DELETE(URI Target, String[] Accept, String[] Expect) {
		return new Request(Target, "DELETE", null, null, Accept, Expect);
	}

	protected Request(URI Target, String Method, 
			  String PayloadType, byte[] Payload,
			  String[] Accept, String[] Expect) {
		target = Target;
		method = Method;
		accept = Accept;
		expect = Expect;
		payloadType = PayloadType;
		payload = Payload;
		headers = new java.util.Properties();
	}

	public void addHeader(String header, String value) {
		assert(header.compareToIgnoreCase("Accept") != 0);
		assert(header.compareToIgnoreCase("Expect") != 0);
		if (headers.getProperty(header) == null)
			headers.put(header, value);
		else
			headers.put(header,
				    headers.getProperty(header) + 
				    ", " + value);
	}

	protected URI target;
	protected String method;
	protected String[] accept;
	protected String[] expect;
	
	protected String payloadType; /* MIME-type */
	protected byte[] payload;

	protected java.util.Properties headers;

	public byte[] Render() {
		String r = method + " " + target + " HTTP/1.1\r\n" +
			"Client: snbench-http-client/0.20050720\r\n" +
			"Connection: close\r\n";
		if (payload != null) {
			r += "Content-Type: " + payloadType + "\r\n" +
				"Content-Length: " + payload.length + "\r\n";
		}
		if ((accept != null) && (accept.length > 0)) {
			r += "Accept: ";
			for (int i=0 ; i<accept.length ; i++) {
				if (i!=0)
					r += ", ";
				r += accept[i];
			}
		}
		if ((expect != null) && (expect.length > 0)) {
			r += "Expect: ";
			for (int i=0 ; i<expect.length ; i++) {
				if (i!=0)
					r += ", ";
				r += expect[i];
			}
			r += "\r\n";
		}

		java.util.Enumeration e = headers.propertyNames();
		while (e.hasMoreElements()) {
			String value = (String) e.nextElement();
			r += value + ": " + 
				headers.getProperty(value) + "\r\n";
		}

		r += "\r\n";
		return r.getBytes();
	}
}
